//
//  HUBezierLine.m
//  贝塞尔曲线
//
//  Created by Mac on 16/3/16.
//  Copyright © 2016年 HY. All rights reserved.
//

#import "HUBezierLine.h"
@interface HUBezierLine ()
{
    CGPoint _globalStart;
    CGPoint _globalEnd;
    CGPoint _startPoint;
    CGPoint _endPoint;
}
@property (nonatomic, strong) CAShapeLayer* shapeLayer;
@property (nonatomic, strong) UIBezierPath *path;
@property (nonatomic, strong) CADisplayLink *displayLink;
@property (nonatomic) NSUInteger loopCount;


@end
static NSString *startPointPostion = @"startPointPostion";
static NSString *endPointPostion   = @"endPointPostion";

@implementation HUBezierLine

- (instancetype) initWithStartPoint:(CGPoint)start EndPoint:(CGPoint)end{
    self = [super init];
    if (self) {
        _startPoint = start;
        _endPoint   = end;
        _shapeLayer = [CAShapeLayer layer];
        _shapeLayer.frame = self.frame;
        _shapeLayer.position = self.center;
        //默认给一个黑色和宽度 1.0
        _shapeLayer.lineWidth = 1.f;
        _shapeLayer.strokeColor = [UIColor blackColor].CGColor;
        
        _shapeLayer.fillColor = [UIColor blackColor].CGColor;
        UIBezierPath* bezier = [UIBezierPath bezierPath];
        [bezier moveToPoint:start];
        [bezier addLineToPoint:end];
        [bezier closePath];
        _path = bezier;
        _shapeLayer.path = bezier.CGPath;
        [self.layer addSublayer:_shapeLayer];
    }
    return self;
}
- (void)startDisplayLink
{
    self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(handleDisplayLink:)];
    [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
- (void)stopDisplayLink
{
    [self.displayLink invalidate];
    self.displayLink = nil;
}
- (void)moveWithStartPoint:(CGPoint)start EndPoint:(CGPoint)end {
    _globalStart = start;
    _globalEnd   = end;
    if (_globalStart.x == _startPoint.x && _globalStart.y == _startPoint.y&& _globalEnd.x == _endPoint.x && _globalEnd.y == _endPoint.y) {
        return;
    }
    if (!self.displayLink ) {
        [self startDisplayLink];
    }
}
- (void)handleDisplayLink:(CADisplayLink *)displayLink
{
    
    static NSInteger tmpCount = 0;
    tmpCount ++;
    CGFloat tmpStartX = (_globalStart.x - _startPoint.x) / 60.;
    CGFloat tmpStartY = (_globalStart.y - _startPoint.y) / 60.;
    
    CGFloat tmpEndX   = (_globalEnd.x - _endPoint.x) / 60.;
    CGFloat tmpEndY   = (_globalEnd.y - _endPoint.y) / 60.;
    
    CGPoint tmpStart = CGPointMake(_startPoint.x + tmpStartX * tmpCount , _startPoint.y + tmpStartY * tmpCount);
    CGPoint tmpEnd   = CGPointMake(_endPoint.x + tmpEndX * tmpCount , _endPoint.y + tmpEndY * tmpCount);

    self.shapeLayer.path  = [self pathAtStartPoint:tmpStart EndPoint:tmpEnd].CGPath;
    if (tmpCount ==  60) {
        [self stopDisplayLink];
        _startPoint = _globalStart;
        _endPoint   = _globalEnd;
        tmpCount =0 ;
    }
}
- (UIBezierPath *)pathAtStartPoint:(CGPoint)start EndPoint:(CGPoint)end{
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:start];
    [path addLineToPoint:end];
    [path closePath];
    return path;
}
#pragma mark Set && Get
- (void)setLineColor:(UIColor *)lineColor {
    _lineColor = lineColor;
    _shapeLayer.strokeColor = lineColor.CGColor;
}
- (void)setLineWidth:(CGFloat)lineWidth {
    _lineWidth = lineWidth;
    _shapeLayer.lineWidth = lineWidth;
}

@end

